/**
 * Copyright (C) 2010 Telecom Digital
 */
package cc.telecomdigital.tdstock.activity.chart;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import cc.mango.android.chartdata.ChartDataHelper;
import cc.mango.android.chartutil.ChartViewFactory;
import cc.mango.android.dto.StkDaily;
import cc.mango.android.dto.StkIntraday;
import cc.mango.common.FinalKey;
import cc.telecomdigital.tdstock.BitmapResizer;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

public class IndexChartActivity extends NvChart
{

	private static String	dateType	= FinalKey.INTRADAY_DATETYPE_TODAY;

	private static boolean	isRsi	= false;							//false : vol    true :  Rsi
	private static boolean	isCs		= false;							//false : line   true :  Vs
	private static boolean	isSma	= false;							//false :not ma   true :   ma
	private static boolean	isDaily	= false;							//false :1day 1week     true :   over 1month

	public enum Type
	{

		A, //股票类型，上下图表都有，Intraday的没有Candlestick图
		B, //外汇类型，只有上面的chart1
		C, D,

	}

	private final Set<String>	A_ITEM_CODES	= new HashSet<String>();
	private final Set<String>	B_ITEM_CODES	= new HashSet<String>();
	private final Set<String>	C_ITEM_CODES	= new HashSet<String>();
	private final Set<String>	D_ITEM_CODES	= new HashSet<String>();

	private Type				CURRENT_TYPE	= Type.A;				//当前图表的类型 

	private boolean			hasDownChart	= true;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.nv_chart);
		TDStockLog.d("Print_Info", "=========onCreate========");
		/*this.bm = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.item_chart_price_column);//by zhengan
*/
		this.bm =  BitmapResizer.decodeImage(this.getResources(),
				R.drawable.item_chart_price_column, 79);  //its size : 79 X 26
		
		/*this.circle = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.nvchart_circle);*/
		
		this.circle  =   BitmapResizer.decodeImage(this.getResources(),
				R.drawable.nvchart_circle, 14);  //its size : 14 X 14

		/*sunFlower = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.nvchart_sunflower);*/
		
		sunFlower =  BitmapResizer.decodeImage(this.getResources(),
				R.drawable.nvchart_sunflower, 35);  //its size : 35 X 35

		findViews();

		getRes();

		initItemCodes();

		theItemCode = this.getIntent().getStringExtra(FinalKey.ITEMCODE);

		//by zhengan
		if (theItemCode == null)
		{
			TDStockLog.d("Print_Info", "===== the theItemCode is null");
			theItemCode = "1";
		}

		if (A_ITEM_CODES.contains(theItemCode))
		{

			CURRENT_TYPE = Type.A;

			initATypeChart();

		}
		else if (B_ITEM_CODES.contains(theItemCode))
		{

			CURRENT_TYPE = Type.B;

			initBTypeChart();

		}
		else if (C_ITEM_CODES.contains(theItemCode))
		{

			CURRENT_TYPE = Type.C;

			initCTypeChart();

		}
		else if (D_ITEM_CODES.contains(theItemCode))
		{

			CURRENT_TYPE = Type.D;

			initDTypeChart();

		}

		updateTitle();

		setListeners();

		chartDataHelper = new ChartDataHelper(theItemCode, this
				.getApplicationContext(), getActivationCode());//by zhengan

		reviseChartTextSize();

		TDStockLog.d("Print_Info", "itemCode:" + theItemCode);
		TDStockLog.d("Print_Info", "_stkname:" + _stkname);
		TDStockLog.d("Print_Info", "CURRENT_TYPE:" + CURRENT_TYPE);

	}

	private void updateTitle()
	{
		String stockName = ((ITDLApplication) this.getApplicationContext())
				.getCurrentStockName();

		if (stockName != null)
		{
			stockName = stockName.replaceAll("[\n\r]", "").replaceAll(
					"(<br>)|(<BR>)", "");
		}
		_stkname.setText(stockName == null ? "" : stockName);
	}

	@Override
	protected void setImageButtonListeners()
	{

		ImageButton.OnClickListener btLineListener = new Button.OnClickListener()
		{
			public void onClick(View v)
			{

				isCs = false;

				changeAllImageButtonStatus();

				drawAboveChart(getDateType());

			}
		};

		ImageButton.OnClickListener btCsListener = new Button.OnClickListener()
		{
			public void onClick(View v)
			{

				isCs = true;

				changeAllImageButtonStatus();

				drawAboveChart(getDateType());

			}
		};

		ImageButton.OnClickListener btSmaListener = new Button.OnClickListener()
		{
			public void onClick(View v)
			{

				isSma = !isSma;

				changeAllImageButtonStatus();

				drawAboveChart(getDateType());
			}
		};

		ImageButton.OnClickListener btVolListener = new Button.OnClickListener()
		{
			public void onClick(View v)
			{

				isRsi = false;
				changeAllImageButtonStatus();
				drawDownChart(getDateType());

			}
		};

		ImageButton.OnClickListener btRsiListener = new Button.OnClickListener()
		{
			public void onClick(View v)
			{

				isRsi = true;

				changeAllImageButtonStatus();

				drawDownChart(getDateType());

			}
		};

		_btline.setOnClickListener(btLineListener);
		_btcandlestick.setOnClickListener(btCsListener);
		_btsma.setOnClickListener(btSmaListener);
		_btvol.setOnClickListener(btVolListener);
		_btrsi.setOnClickListener(btRsiListener);
	}

	protected void setDateType(String dateType)
	{

		IndexChartActivity.dateType = dateType;

	}

	public String getDateType()
	{

		return dateType;

	}

	protected void handleImageButtonStatus()
	{

		if (FinalKey.INTRADAY_DATETYPE_TODAY.equals(getDateType())
				|| FinalKey.INTRADAY_DATETYPE_WEEK.equals(getDateType()))
		{

			isDaily = false;

			isRsi = false;//再次切换为Daily数据时，回到!isRsi，即不是Rsi图

			//  isSma = false;//再次切换为Daily数据时，isSma不改变，与先前状态一致

			isCs = false;

			changeAllImageButtonStatus();

		}
		else
		{

			isDaily = true;

			changeAllImageButtonStatus();
		}

	}

	protected void changeAllImageButtonStatus()
	{

		setImageButtonStatus(_btrsi, !isRsi && isDaily && hasDownChart);
		setImageButtonStatus(_btvol, isRsi && isDaily && hasDownChart);

		setImageButtonStatus(_btcandlestick, !isCs && isDaily);
		setImageButtonStatus(_btline, isCs && isDaily);

		setImageButtonStatus(_btsma, isDaily);

	}

	protected View getDownView(List datalist, String datetype)
	{

		View view;

		if (isRsi && isDaily)
		{

			view = ChartViewFactory.getStkRsiChartView(
					IndexChartActivity.this, datalist);

		}
		else
		{

			view = ChartViewFactory.getStkVolChartView(
					IndexChartActivity.this, datalist);
		}

		return view;

	}

	protected View getUpView(List datalist, String dateType)
	{

		View view = null;

		if (isCs && isDaily)
		{

			if (isSma && isDaily)
			{

				view = ChartViewFactory.getCandlestickSmaChartView(
						IndexChartActivity.this, datalist, dateType);
			}
			else
			{

				view = ChartViewFactory.getCandlestickChartView(
						IndexChartActivity.this, datalist, dateType);
			}

		}
		else
		{

			if (isSma && isDaily)
			{

				view = ChartViewFactory.getStkLineSmaChartView(
						IndexChartActivity.this, datalist, dateType);
			}
			else
			{

				view = ChartViewFactory.getStkLineChartView(
						IndexChartActivity.this, datalist, dateType);
			}

		}

		return view;
	}

	protected void getUpAndDownView(List dataList, String dateType)
	{

		_aboveView = getUpView(dataList, dateType);

		if (hasDownChart)
		{

			_underView = getDownView(dataList, dateType);
		}

	}

	protected void initItemCodes()
	{

		String[] A_itemCodes = new String[] { "HSI", "HSI:1", "HSI:2",
				"HHI:1", "HHI:2", "SSE1" };

		String[] B_itemCodes = new String[] { "HSCEI", "N225", "FTSE", "DJI",
				"FTSE" }; //"FTSE"重复

		String[] C_itemCodes = new String[] { "SSI:1", "SSI:2" };

		String[] D_itemCodes = new String[] { "GDAX", "DJI:1", "GDAX" }; //"GDAX"重复

		for (String code : A_itemCodes)
		{

			A_ITEM_CODES.add(code);

		}

		for (String code : B_itemCodes)
		{

			B_ITEM_CODES.add(code);

		}

		for (String code : C_itemCodes)
		{

			C_ITEM_CODES.add(code);

		}

		for (String code : D_itemCodes)
		{

			D_ITEM_CODES.add(code);

		}

		TDStockLog.d("Print_Info", "" + A_ITEM_CODES.size());
		TDStockLog.d("Print_Info", "" + B_ITEM_CODES.size());
		TDStockLog.d("Print_Info", "" + C_ITEM_CODES.size());
		TDStockLog.d("Print_Info", "" + D_ITEM_CODES.size());

	}

	//A Type chart,默认情况，没有什么可做的
	private void initATypeChart()
	{

	}

	//B Type chart
	private void initBTypeChart()
	{

		hideDownChart();//隐藏下面的图表
	}

	//C Type chart
	private void initCTypeChart()
	{

		hideOtherDateButton();//只保留“1天”按钮

		setDateType(FinalKey.INTRADAY_DATETYPE_TODAY);//切换为“1天”的图表

	}

	//D Type chart
	private void initDTypeChart()
	{

		hideOtherDateButton();//只保留“1天”按钮

		hideDownChart();//隐藏下面的图表

		setDateType(FinalKey.INTRADAY_DATETYPE_TODAY);//切换为“1天”的图表
	}

	private void hideDownChart()
	{

		//		 	LinearLayout nv_chart = (LinearLayout) this.getLayoutInflater().inflate(R.layout.nv_chart, null);

		LinearLayout chartView = (LinearLayout) this
				.findViewById(R.id.chartview);
		LinearLayout sunFlowerView = (LinearLayout) this
				.findViewById(R.id.sunflower_view);
		//		    LinearLayout nodataView = (LinearLayout) this.findViewById(R.id.nodata_view);

		//		    LinearLayout chartView = (LinearLayout) nv_chart.findViewById(R.id.chartview);
		//		    LinearLayout sunFlowerView = (LinearLayout) nv_chart.findViewById(R.id.sunflower_view);
		//		    LinearLayout nodataView = (LinearLayout) nv_chart.findViewById(R.id.nodata_view);

		chartView.removeView(_underContainer);
		sunFlowerView.removeView(downSunFlowerContainer);
		//		    nodataView.removeView(downNoDataContainer);

		dview = null;//不启动下面图表的sunflower

		hasDownChart = false;

	}

	private void hideOtherDateButton()
	{

		//		 	_bt1day.setVisibility(View.INVISIBLE);
		_bt1week.setVisibility(View.INVISIBLE);
		_bt1month.setVisibility(View.INVISIBLE);
		_bt3months.setVisibility(View.INVISIBLE);
		_bt6months.setVisibility(View.INVISIBLE);
		_bt1year.setVisibility(View.INVISIBLE);
		_bt2years.setVisibility(View.INVISIBLE);
	}

	public void updateStkIntradayInfoView(StkIntraday stk)
	{

		String dateTime = stk.getDtyyyymmdd().substring(4, 6) + "月"
				+ stk.getDtyyyymmdd().substring(6, 8) + "日  "
				+ stk.getTime().substring(0, 2) + ":"
				+ stk.getTime().substring(2, 4);

		updateTextView(_tv_labels[1], dateTime);

		if (hasDownChart)
		{

			String vol = getStringVol(stk.getVol());

			updateTextView(_tv_labels[2], _res_strings[0] + vol);

		}

		updateTextView(_tv_labels[3], "");

		updateTextView(_tv_labels[4], "");

		updateTextView(_tv_labels[5], "");
	}

	public void updateStkDailyInfoView(StkDaily stk)
	{

		String dateTime = stk.getDtyyyymmdd().substring(2, 4) + "年"
				+ stk.getDtyyyymmdd().substring(4, 6) + "月"
				+ stk.getDtyyyymmdd().substring(6, 8) + "日";

		TDStockLog.d(TAG, "" + stk.toString());

		updateTextView(_tv_labels[1], dateTime);

		if (hasDownChart)
		{

			if (isRsi)
			{

				String rsi = getStringRsi(stk.getRsi());

				updateTextView(_tv_labels[2], _res_strings[4] + rsi);

			}
			else
			{

				String vol = getStringVol(stk.getVol());

				updateTextView(_tv_labels[2], _res_strings[0] + vol);

			}

		}

		String high = getStringPrice(stk.getHigh());

		updateTextView(_tv_labels[3], _res_strings[1] + high);

		String low = getStringPrice(stk.getLow());

		updateTextView(_tv_labels[4], _res_strings[2] + low);

		String open = getStringPrice(stk.getOpen());

		updateTextView(_tv_labels[5], _res_strings[3] + open);
	}

	@Override
	protected void initChartLayout()
	{
		initUpChartLayout();

		if (hasDownChart)
		{

			initDownChartLayout();

		}
		//			initDownChartLayout();

	}

	@Override
	public void refreshView()
	{
		this.setAllImageViewVisibility(!getIsTouching());//是否隐藏goup A 和 group B

		_aboveView.postInvalidate();

		if (hasDownChart)
		{

			_underView.postInvalidate();

		}

		TDStockLog.d("Print_Info", "index refresh");

		if (getIsTouching())
		{

			this.changeVisibility();

			this.updateTextViewByPoint(index, dateList);
		}
		else
		{

			this.restoreVisibility();
		}

	}

}